package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.CartMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.CartService;
import com.sky.service.DishService;
import com.sky.utils.ThreadLocalUtil;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CartServiceimpl implements CartService {

    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param cartDTO
     */
    @Override
    public void add(ShoppingCartDTO cartDTO) {
        log.info("添加购物车：{}", cartDTO);
        //创建一个购物车对象 判断购物车中是否有该菜品或套餐
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(cartDTO,shoppingCart);
        shoppingCart.setUserId(ThreadLocalUtil.getCurrentUser());
        List<ShoppingCart> list = cartMapper.Query(shoppingCart);
        //如果数据库有该菜品或套餐，则数量加1
        if(list!=null && list.size()>0){
            shoppingCart = list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            log.info("添加的购物车数据存在，数量加1");
            cartMapper.update(shoppingCart);
        }else {
            //如果数据库没有该菜品或套餐，则添加
            //判断添加的是菜品还是套餐
            if(cartDTO.getDishId()!=null){
                //添加的是菜品
                DishVO byId = dishMapper.getById(cartDTO.getDishId());
                shoppingCart.setName(byId.getName());
                shoppingCart.setImage(byId.getImage());
                shoppingCart.setDishFlavor(cartDTO.getDishFlavor());
                shoppingCart.setAmount(byId.getPrice());

            }else {
                //添加的是套餐
                Setmeal setmeal = setmealMapper.getById(cartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());

            }
            shoppingCart.setUserId(ThreadLocalUtil.getCurrentUser());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            cartMapper.insert(shoppingCart);

        }


    }

    /**
     * 查看购物车
    **/
    @Override
    public List<ShoppingCart> showCart() {
        Long userId = ThreadLocalUtil.getCurrentUser();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);

        List<ShoppingCart> shoppingCarts = cartMapper.Query(shoppingCart);
        return shoppingCarts;
    }

    @Override
    public void clean() {
        cartMapper.clean(ThreadLocalUtil.getCurrentUser());
    }

    @Override
    public void sub(ShoppingCartDTO cartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        // 获取当前用户
        Long userId = ThreadLocalUtil.getCurrentUser();
        BeanUtils.copyProperties(cartDTO, shoppingCart);
        shoppingCart.setUserId(userId);

        // 查询当前菜品或套餐是否在购物车中
        List<ShoppingCart> shoppingCarts = cartMapper.Query(shoppingCart);

        // 如果存在
        if(shoppingCarts != null && shoppingCarts.size() > 0){
            ShoppingCart cart = shoppingCarts.get(0);
            if(cart.getNumber()  ==1){
                //如果数量为1，则从购物车中删除该记录
                cartMapper.deleteById(cart.getId());
            }else if(cart.getNumber() > 1){
                //如果数量大于1，则更新购物车中的数量
                cart.setNumber(cart.getNumber() - 1);
                cartMapper.update(cart);
            }
        }

    }
}
